import Vue from 'vue'
import Vuex from 'vuex'

import socket from '../common/socket.js'

Vue.use(Vuex)

const state = {
  info: []
}

const getters = {
	change: state => state.info
}

const mutations = {
  send(state, data) { //发送消息
    socket.emit("pushMsg", data);
  },
  changeInfo(state, data) { //接收到消息改变消息数组 1是对方消息 2是我方消息
    state.info.push(data)
  }
}

const actions = {
  getInfo(context) { //接收消息
    socket.on('pushMsg', (data) => {
			console.log(data);
      context.commit('changeInfo', data)
    })
  }
}

const store = new Vuex.Store({
  state,
	mutations,
	actions,
	getters
})

export default store